<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-row :gutter="20">
        <el-col :span="24" align="right">
          <el-form-item label="物料编码" prop="ksCode">
            <el-input
              v-model="queryParams.wlCode"
              placeholder="请输入物料编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-consume"
          :disabled="multiple"
          @click="handleConsume"
          v-hasPermi="['warehouse:ylkc:consume']"
        >消耗记录</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          size="mini"
          icon="el-icon-increase"
          :disabled="multiple"
          @click="handleIncrease"
          v-hasPermi="['warehouse:ylkc:increase']"
        >来料记录</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          icon="el-icon-all"
          :disabled="multiple"
          @click="handleInventory"
          v-hasPermi="['warehouse:ylkc:inventory']"
        >库存详情</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ylkcList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物料编码" align="center" prop="wldaMaterialFile.wlCode" />
      <el-table-column label="物料名称" align="center" prop="wldaMaterialFile.wlName" />
      <el-table-column label="物料规格" align="center" prop="wldaMaterialFile.wlGg" />
      <el-table-column label="物料分类" align="center" prop="wldaMaterialFile.wlTypecode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wl_from" :value="scope.row.wldaMaterialFile.wlTypecode"/>
        </template>
      </el-table-column>
      <el-table-column label="物料单位" align="center" prop="wldaMaterialFile.jldwMeasurementUnit.dwName" />
      <el-table-column label="物料总数" align="center" prop="havenum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-consume"
            @click="handleConsume(scope.row)"
            v-hasPermi="['warehouse:ylkc:consume']"
          >消耗记录</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-increase"
            @click="handleIncrease(scope.row)"
            v-hasPermi="['warehouse:ylkc:increase']"
          >来料记录</el-button>
          <el-button
          size="mini"
          type="text"
          icon="el-icon-all"
          @click="handleInventory(scope.row)"
          v-hasPermi="['warehouse:ylkc:inventory']"
        >库存详情</el-button>
        </template>
      </el-table-column>
    </el-table>

<!--    分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--     弹框 -->
    <el-dialog :title="PopUpsTitle" :visible.sync="open1" width="45%">
      <el-container>
        <el-header>
          <el-input suffix-icon="el-icon-search" @keyup.enter.native="getPeoValDirector"></el-input>
        </el-header>
        <!--展示来料记录-->
        <el-main v-if="increaseList !== null">
          <el-table  border :data="increaseList" style="text-align: center;" align="center">
            <el-table-column prop="llrkIncomingMaterialStorage.llCode" label="来料批次" width="180px" align="center"></el-table-column>
            <el-table-column prop="llrkIncomingMaterialStorage.wldaMaterialFile.wlName" label="来料名称" align="center"></el-table-column>
            <el-table-column prop="ckWarehouse.ckName" label="入库仓库" align="center"></el-table-column>
            <el-table-column label="来料时间" prop="llrkIncomingMaterialStorage.gettime" width="140" align="center">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.llrkIncomingMaterialStorage.gettime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column prop="llrkIncomingMaterialStorage.llrkPersonnel" label="入库人员" align="center"></el-table-column>
            <el-table-column prop="llrkIncomingMaterialStorage.ksMerchants.merchantsName" label="供应商" align="center"></el-table-column>
            <el-table-column prop="innum" label="入库数量" align="center"></el-table-column>
          </el-table>
          <pagination
            v-show="queryParams1.total>0"
            :total="queryParams1.total"
            :page.sync="queryParams1.pageNum"
            :limit.sync="queryParams1.pageSize"
            @pagination="handleIncrease"
          />
        </el-main>
        <!--展示库存详情-->
        <el-main v-if="inventoryList !== null">
          <el-table  border :data="inventoryList" style="text-align: center;">
            <el-table-column prop="wlCode" label="物料编码" width="180px" align="center"></el-table-column>
            <el-table-column prop="wldaMaterialFile.wlName" label="物料名称"  align="center"></el-table-column>
            <el-table-column prop="wldaMaterialFile.wlGg" label="物料规格" align="center"></el-table-column>
            <el-table-column prop="ckWarehouse.ckCode" label="仓库编码" align="center"></el-table-column>
            <el-table-column prop="ckWarehouse.ckName" label="仓库名称" align="center"></el-table-column>
            <el-table-column prop="wlLevel" label="物料等级" align="center"></el-table-column>
            <el-table-column prop="havenum" label="库存数量" align="center"></el-table-column>
          </el-table>
          <pagination
            v-show="queryParams1.total>0"
            :total="queryParams1.total"
            :page.sync="queryParams1.pageNum"
            :limit.sync="queryParams1.pageSize"
            @pagination="handleInventory"
          />
        </el-main>
        <!--展示消耗记录-->
        <el-main v-if="consumeList !== null ">
          <el-table  border :data="consumeList" style="text-align: center;">
            <el-table-column label="领料单号" prop="scllProductionMaterialRequisition.lldh" width="110px" align="center"></el-table-column>
            <el-table-column label="领料时间" prop="scllProductionMaterialRequisition.applicationTime" width="100"  align="center" >
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.scllProductionMaterialRequisition.applicationTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="生产工单" prop="scllProductionMaterialRequisition.scgdProductionWorkOrder.gdCode" align="center" width="110"></el-table-column>
            <el-table-column label="领用人" align="center" prop="scllProductionMaterialRequisition.proposer"></el-table-column>
            <el-table-column label="仓库名称" prop="ckWarehouse.ckName" align="center"></el-table-column>
            <el-table-column label="物料批次" prop="llCode" width="170" align="center"></el-table-column>
            <el-table-column label="物料等级" prop="llslLevel" align="center"></el-table-column>
            <el-table-column label="消耗数量" prop="getNum" align="center"></el-table-column>
          </el-table>
          <pagination
            v-show="queryParams1.total>0"
            :total="queryParams1.total"
            :page.sync="queryParams1.pageNum"
            :limit.sync="queryParams1.pageSize"
            @pagination="handleConsume"
          />
        </el-main>
      </el-container>
    </el-dialog>

  </div>
</template>

<script>
import {increaseList, inventoryList, listYlkc,consumeList} from "@/api/warehouse/ylkc";

export default {
  name: "ylkc",
  dicts: ['wl_from', 'is_use', 'wl_type', 'wl_sortcode'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWldaMaterialFile: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 原料库存表格数据
      ylkcList: [],
      //库存详情
      inventoryList:null,
      //来料记录
      increaseList:null,
      //消耗记录
      consumeList:null,
      // 弹出层标题
      PopUpsTitle: null,
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（客商选择）
      open1: false,
      // 弹床标题

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        llCode: null,
        wlCode: null
      },
      // 查询客商参数
      queryParams1: {
        pageNum: 1,
        pageSize: 10,
        total:0,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询来料入库列表 */
    getList() {
      this.reset();
      this.loading = true;
      listYlkc(this.queryParams).then(response => {
        this.ylkcList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        llCode: null,
        ksCode: null,
        wlCode: null,
        getTime: null,
        isUse: null,
        isDelete: null,
        total: null,
        wldaMaterialFile: {
          wlName:null,
          wlCode:null
        },
        ksMerchants:{
          ksCode:null,
          merchantsName:null
        }
      };
      this.consumeList=null;
      this.increaseList=null;
      this.inventoryList=null;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.wldaMaterialFile.wlCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加来料入库";
    },
    /** 消耗记录按钮操作 */
    handleConsume(row) {
      this.reset()
      this.PopUpsShow()
      const wlCodes = row.wlCode || this.ids
        consumeList(wlCodes).then(response => {
          // console.log(response.rows)
          this.consumeList=response.rows
          this.title = "消耗记录";
          this.queryParams1.total = response.total;
      });
    },
    /** 来料记录按钮操作 */
    handleIncrease(row) {
      this.reset()
      this.PopUpsShow()
      const wlCodes = row.wlCode || this.ids
      increaseList(wlCodes).then(response => {
        this.increaseList=response.rows
        this.title = "来料记录";
        this.queryParams1.total = response.total;
      });
    },
    /** 库存详情按钮操作 */
    handleInventory(row) {
      this.reset()
      this.PopUpsShow()
      const wlCodes = row.wlCode || this.ids
      inventoryList(wlCodes).then(response => {
        this.inventoryList=response.rows
        this.title = "库存详情";
        this.queryParams1.total = response.total;
      });
    },
    /** 弹窗 */
    PopUpsShow(){
      this.open1=true
      this.loading = false;
    },
    /** 物料档案序号 */
    rowWldaMaterialFileIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 复选框选中数据 */
    handleWldaMaterialFileSelectionChange(selection) {
      this.checkedWldaMaterialFile = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('warehouse/llrk/export', {
        ...this.queryParams
      }, `llrk_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
